package vip.liux.backend.application.service.shop;

import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;
import vip.liux.backend.domain.models.shop.address.UserAddress;
import vip.liux.contracts.infrastructure.automap.IgnoreEntity;
import vip.liux.contracts.infrastructure.automap.MapMapper;
import vip.liux.contracts.infrastructure.automap.MapStructConfig;

import java.util.Map;

@Mapper(config = MapStructConfig.class, uses = {MapMapper.class})
public interface AutoMapperProfile {

    AutoMapperProfile INSTANCE = Mappers.getMapper(AutoMapperProfile.class);

    @Mapping(target = "city", expression = "java(regions.get(Long.valueOf(source.getCity())).getName())")
    @Mapping(target = "province", expression = "java(regions.get(Long.valueOf(source.getProvince())).getName())")
    @Mapping(target = "district", expression = "java(regions.get(Long.valueOf(source.getDistrict())).getName())")
    @Mapping(target = "provinceId", source = "province")
    @Mapping(target = "cityId", source = "city")
    @Mapping(target = "districtId", source = "district")
    AddressDto toAddressDto(UserAddress source, @Context Map<Long, RegionDto> regions);

    @IgnoreEntity
    @Mapping(target = "userId", ignore = true)
    @Mapping(target = "defaultAddress", ignore = true)
    UserAddress toUserAddress(ModifyingAddressDto source);

    @IgnoreEntity
    @Mapping(target = "userId", ignore = true)
    @Mapping(target = "defaultAddress", ignore = true)
    void toUserAddress(ModifyingAddressDto source, @MappingTarget UserAddress target);
}
